package com.nut.spp.config;

import com.nut.spp.interceptor.AdminLoginInterceptor;
import com.nut.spp.interceptor.ClientLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Autowired
    private ClientLoginInterceptor clientLoginInterceptor;

    @Autowired
    private AdminLoginInterceptor adminLoginInterceptor;

    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        /* 是否允许请求带有验证信息 */
        corsConfiguration.setAllowCredentials(true);
        /* 允许访问的客户端域名 */
        corsConfiguration.addAllowedOrigin("*");
        /* 允许服务端访问的客户端请求头 */
        corsConfiguration.addAllowedHeader("*");
        /* 允许访问的方法名,GET POST等 */
        corsConfiguration.addAllowedMethod("*");
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    //不需要拦截
    final String[] adminNot = {
            "/admin/system/**",
            "/admin/auth/login"
    };

    final String[] adminNeeds = {
            "/admin/auth/info",
            "/admin/dict/**",
            "/admin/menu/**",
            "/admin/dept/**",
            "/admin/pay/**",
            "/admin/role/**",
            "/admin/dept/**",
            "/admin/user/**",
            "/admin/brand/**",
            "/admin/couponWhite/**",
            "/admin/category/**",
            "/admin/dict/**",
            "/admin/goods/**",
            "/admin/goodsSpec/**",
            "/admin/member/**",
            "/admin/param/**",
            "/admin/region/**",
            "/admin/shop/**",
            "/admin/order/**"
    };

    final String[] clientNot = {
            "/client/goods/**",
            "/client/goodsSpec/**",
            "/client/category/**",
            "/client/auth/login",
            "/client/pay/pay",
            "/client/pay/refund",
            "/client/order/money/**",
            "/client/order/status/**",
            "/client/auth/imgCode"
    };

    final String[] clientNeeds = {
            "/client/auth/info",
            "/client/auth/bindWxMobile",
            "/client/member/**",
            "/client/memberAddress/**",
            "/client/memberCoupon/**",
            "/client/pay/**",
            "/client/shop/**",
            "/client/order/**"
    };

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(adminLoginInterceptor).addPathPatterns(adminNeeds).excludePathPatterns(adminNot);

        registry.addInterceptor(clientLoginInterceptor).addPathPatterns(clientNeeds).excludePathPatterns(clientNot);

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("swagger-ui.html")
//                .addResourceLocations("classpath:/META-INF/resources/");
//        registry.addResourceHandler("/webjars/**")
//                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
